<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'parent', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "parentinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('parent');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanAdd()) {
	$Security->SaveLastUrl();
	Page_Terminate("parentlist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$parent->Export = @$_GET["export"]; // Get export parameter
$sExport = $parent->Export; // Get export parameter, used in header
$sExportFile = $parent->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["parent_id"] != "") {
  $parent->parent_id->setQueryStringValue($_GET["parent_id"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $parent->CurrentAction = $_POST["a_add"]; // Get form action
  GetUploadFiles(); // Get upload files
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $parent->CurrentAction = "C"; // Copy Record
  } else {
    $parent->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($parent->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($parent->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$parent->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($parent->KeyUrl($parent->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$parent->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_parent_name"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Name"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_parent_link"];
		if (elm && !ew_CheckFileType(elm.value)) { 
			if (!ew_OnError(elm, "File type is not allowed.")) 
				return false; 
		}
		elm = fobj.elements["x" + infix + "_parent_order"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Order"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Add to TABLE: parent<br><br><a href="<?php echo $parent->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
  $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
}
?>
<form name="fparentadd" id="fparentadd" action="parentadd.php" method="post" enctype="multipart/form-data" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewTable">
  <tr class="ewTableRow">
    <td class="ewTableHeader">Name<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $parent->parent_name->CellAttributes() ?>><span id="cb_x_parent_name">
<input type="text" name="x_parent_name" id="x_parent_name"  size="30" maxlength="250" value="<?php echo $parent->parent_name->EditValue ?>"<?php echo $parent->parent_name->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Type</td>
    <td<?php echo $parent->parent_type->CellAttributes() ?>><span id="cb_x_parent_type">
<?php
$arwrk = $parent->parent_type->EditValue;
if (is_array($arwrk)) {
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($parent->parent_type->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " checked" : "";
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="radio" name="x_parent_type" id="x_parent_type" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $parent->parent_type->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Link</td>
    <td<?php echo $parent->parent_link->CellAttributes() ?>><span id="cb_x_parent_link">
<input type="file" name="x_parent_link" id="x_parent_link"  size="30"<?php echo $parent->parent_link->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Order</td>
    <td<?php echo $parent->parent_order->CellAttributes() ?>><span id="cb_x_parent_order">
<input type="text" name="x_parent_order" id="x_parent_order"  size="30" maxlength="100" value="<?php echo $parent->parent_order->EditValue ?>"<?php echo $parent->parent_order->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Active</td>
    <td<?php echo $parent->parent_active->CellAttributes() ?>><span id="cb_x_parent_active">
<?php
$arwrk = $parent->parent_active->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($parent->parent_active->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_parent_active[]" id="x_parent_active[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $parent->parent_active->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
  </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    Add    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Function Get upload files
function GetUploadFiles() {
	global $objForm, $parent;

	// Get upload data
		if ($parent->parent_link->Upload->UploadFile()) {

			// No action required
		} else {
			echo $parent->parent_link->Upload->Message;
			exit();
		}
}
?>
<?php

// Load default values
function LoadDefaultValues() {
	global $parent;
	$parent->parent_type->CurrentValue = 0;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $parent;
	$parent->parent_name->setFormValue($objForm->GetValue("x_parent_name"));
	$parent->parent_type->setFormValue($objForm->GetValue("x_parent_type"));
	$parent->parent_order->setFormValue($objForm->GetValue("x_parent_order"));
	$parent->parent_active->setFormValue($objForm->GetValue("x_parent_active"));
}

// Restore form values
function RestoreFormValues() {
	global $parent;
	$parent->parent_name->CurrentValue = $parent->parent_name->FormValue;
	$parent->parent_type->CurrentValue = $parent->parent_type->FormValue;
	$parent->parent_order->CurrentValue = $parent->parent_order->FormValue;
	$parent->parent_active->CurrentValue = $parent->parent_active->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $parent;
	$sFilter = $parent->SqlKeyFilter();
	if (!is_numeric($parent->parent_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@parent_id@", ew_AdjustSql($parent->parent_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$parent->Row_Selecting($sFilter);

	// Load sql based on filter
	$parent->CurrentFilter = $sFilter;
	$sSql = $parent->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$parent->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $parent;
	$parent->parent_id->setDbValue($rs->fields('parent_id'));
	$parent->parent_name->setDbValue($rs->fields('parent_name'));
	$parent->parent_type->setDbValue($rs->fields('parent_type'));
	$parent->parent_link->Upload->DbValue = $rs->fields('parent_link');
	$parent->parent_order->setDbValue($rs->fields('parent_order'));
	$parent->parent_active->setDbValue($rs->fields('parent_active'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $parent;

	// Call Row Rendering event
	$parent->Row_Rendering();

	// Common render codes for all row types
	// parent_name

	$parent->parent_name->CellCssStyle = "";
	$parent->parent_name->CellCssClass = "";

	// parent_type
	$parent->parent_type->CellCssStyle = "";
	$parent->parent_type->CellCssClass = "";

	// parent_link
	$parent->parent_link->CellCssStyle = "";
	$parent->parent_link->CellCssClass = "";

	// parent_order
	$parent->parent_order->CellCssStyle = "";
	$parent->parent_order->CellCssClass = "";

	// parent_active
	$parent->parent_active->CellCssStyle = "";
	$parent->parent_active->CellCssClass = "";
	if ($parent->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($parent->RowType == EW_ROWTYPE_ADD) { // Add row

		// parent_name
		$parent->parent_name->EditCustomAttributes = "";
		$parent->parent_name->EditValue = ew_HtmlEncode($parent->parent_name->CurrentValue);

		// parent_type
		$parent->parent_type->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Intro");
		$arwrk[] = array("2", "List News");
		$arwrk[] = array("3", "Product HD");
		$arwrk[] = array("4", "Movie");
		$arwrk[] = array("5", "Product Dochoi");
		$parent->parent_type->EditValue = $arwrk;

		// parent_link
		$parent->parent_link->EditCustomAttributes = "";
		$parent->parent_link->EditValue = $parent->parent_link->CurrentValue;

		// parent_order
		$parent->parent_order->EditCustomAttributes = "";
		$parent->parent_order->EditValue = ew_HtmlEncode($parent->parent_order->CurrentValue);

		// parent_active
		$parent->parent_active->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$parent->parent_active->EditValue = $arwrk;
	} elseif ($parent->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($parent->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$parent->Row_Rendered();
}
?>
<?php

// Add record
function AddRow() {
	global $conn, $Security, $parent;

	// Check for duplicate key
	$bCheckKey = TRUE;
	$sFilter = $parent->SqlKeyFilter();
	if (trim(strval($parent->parent_id->CurrentValue)) == "") {
		$bCheckKey = FALSE;
	} else {
		$sFilter = str_replace("@parent_id@", ew_AdjustSql($parent->parent_id->CurrentValue), $sFilter); // Replace key value
	}
	if (!is_numeric($parent->parent_id->CurrentValue)) {
		$bCheckKey = FALSE;
	}
	if ($bCheckKey) {
		$rsChk = $parent->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
			$rsChk->Close();
			return FALSE;
		}
	}
	$rsnew = array();

	// Field parent_name
	$parent->parent_name->SetDbValueDef($parent->parent_name->CurrentValue, "");
	$rsnew['parent_name'] =& $parent->parent_name->DbValue;

	// Field parent_type
	$parent->parent_type->SetDbValueDef($parent->parent_type->CurrentValue, NULL);
	$rsnew['parent_type'] =& $parent->parent_type->DbValue;

	// Field parent_link
	$parent->parent_link->Upload->SaveToSession(); // Save file value to Session
	if (is_null($parent->parent_link->Upload->Value)) {
		$parent->parent_link->Upload->DbValue = NULL;
	} else {
		$parent->parent_link->Upload->DbValue = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $parent->parent_link->Upload->FileName);
	}
	$rsnew['parent_link'] =& $parent->parent_link->Upload->DbValue;

	// Field parent_order
	$parent->parent_order->SetDbValueDef($parent->parent_order->CurrentValue, NULL);
	$rsnew['parent_order'] =& $parent->parent_order->DbValue;

	// Field parent_active
	$parent->parent_active->SetDbValueDef($parent->parent_active->CurrentValue, NULL);
	$rsnew['parent_active'] =& $parent->parent_active->DbValue;

	// Call Row Inserting event
	$bInsertRow = $parent->Row_Inserting($rsnew);
	if ($bInsertRow) {

		// Field parent_link
			if (!is_null($parent->parent_link->Upload->Value)) ew_SaveFile(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $rsnew['parent_link'], $parent->parent_link->Upload->Value);
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$AddRow = $conn->Execute($parent->InsertSQL($rsnew));
		$conn->raiseErrorFn = '';
	} else {
		if ($parent->CancelMessage <> "") {
			$_SESSION[EW_SESSION_MESSAGE] = $parent->CancelMessage;
			$parent->CancelMessage = "";
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
		}
		$AddRow = FALSE;
	}
	if ($AddRow) {
		$parent->parent_id->setDbValue($conn->Insert_ID());
		$rsnew['parent_id'] =& $parent->parent_id->DbValue;

		// Call Row Inserted event
		$parent->Row_Inserted($rsnew);
	}

	// Field parent_link
	$parent->parent_link->Upload->RemoveFromSession(); // Remove file value from Session
	return $AddRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
